package com.group02.community.service.impl;


import com.alibaba.excel.EasyExcel;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group02.community.entity.dict.SysDictType;
import com.group02.community.mapper.SysDictTypeMapper;
import com.group02.community.service.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 字典类型表 服务实现类
 * </p>
 *
 * @author xl
 * @since 2022-07-01
 */
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService {

    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;

    @Override
    public IPage<SysDictType> selectAllDictionaries(Long current, Long size, SysDictType sysDictType) {
        IPage<SysDictType> dictTypeIPage =sysDictTypeMapper.selectAllDictionaries(new Page<SysDictType>(current,size),sysDictType);
        return dictTypeIPage;
    }

    @Override
    public void deleteSelectDictionaries(Long[] id) {
        sysDictTypeMapper.deleteSelectDictionaries(id);
    }

    @Override
    public void downloadExcel(HttpServletResponse response) throws IOException {
        //查询所有数据
        List<SysDictType> sysDictTypes = sysDictTypeMapper.selectList(null);
        //设置文件下载
        //设置响应头，告诉浏览器要以文件的形式保存
//        response.setHeader();
        response.setHeader("content-disposition","attachment;filename="+System.currentTimeMillis()+".xlms");
        //生成Excel并写入
        EasyExcel.write(response.getOutputStream(),SysDictType.class).sheet(System.currentTimeMillis()+"").doWrite(sysDictTypes);
    }

    @Override
    public boolean hasData(Long[] id) {
        List<SysDictType> sysDictTypes = sysDictTypeMapper.selectBatchIds(Arrays.asList(id));
        List<String> dictTypeList = new ArrayList<>();
        sysDictTypes.forEach(a ->{
            dictTypeList.add(a.getDictType());
        });
        return sysDictTypeMapper.hasData(dictTypeList).isEmpty();
    }
}
